package uit.edu.vn.Utility;

import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Activity.R;
import uit.edu.vn.Model.myFile;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter{
	
	private Context context;
	private List<myFile> fileList;
	private LayoutInflater mInflater;

	private ArrayList<Boolean> itemFileChecked = new ArrayList<Boolean>();
	public FileListAdapter(Context context , List<myFile> fileList) 
	{
		// TODO Auto-generated constructor stub
		
		this.context=context;
		this.fileList=fileList;
		mInflater= LayoutInflater.from(context);
		// thiêt lap mac dinh itemcheck là false
		for (int i = 0; i < this.getCount(); i++) {
		       itemFileChecked.add(i, false);
		   }
	}
	@Override
	public int getCount() 
	{
		// TODO Auto-generated method stub
		return fileList.size();
	}
	@Override
	public Object getItem(int position) 
	{
		// TODO Auto-generated method stub
		return fileList.get(position);
	}
	 @Override
	public long getItemId(int position) 
	 {
		// TODO Auto-generated method stub
		return position;
	}
	 @Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	 {
		 myFile entry = fileList.get(position); 
		 // TODO Auto-generated method stub
		 if(convertView==null)
		 {
			 convertView=mInflater.inflate(R.layout.row_file, null);
		 }
		 TextView txt_nameFile = (TextView)convertView.findViewById(R.id.txt_fileName);
		 TextView txt_pathFile= (TextView)convertView.findViewById(R.id.txt_filePath);
		 ImageView img_iconFile= (ImageView)convertView.findViewById(R.id.img_fileIcon);
		 txt_nameFile.setText(entry.getNameFile());
		 txt_pathFile.setText(entry.getPathFile());
		 //set icon 
		 img_iconFile.setImageDrawable(entry.getIconFile());	
		// File file=new File()
		 //xy lu check box 
		 CheckBox cbox= (CheckBox)convertView.findViewById(R.id.chkSelectFile);
		 cbox.setOnClickListener(new OnClickListener() {
		        public void onClick(View v) {
		            CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelectFile);

		            if (cb.isChecked()) 
		            {
		                itemFileChecked.set(position, true);
	
		            } else if (!cb.isChecked()) 
		            {
		                itemFileChecked.set(position, false);
		
		            }
		        }
		    });

		cbox.setChecked(itemFileChecked.get(position));
		
		return convertView;
	}
	public ArrayList<Boolean> getCheckList()
	{
		return itemFileChecked;
	}
	public ArrayList<Boolean> setCheckList(ArrayList<Boolean> itemCheck)
	{
		itemFileChecked=itemCheck;
		return itemFileChecked;
	}
}
